
#include "Simple_window.h"
#include "Graph.h"
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Text_Display.H>

#include "Store_menu.h"
#include <iostream>

using namespace std;
using namespace Store;
using namespace Graph_lib;

//types we need

namespace Graph_lib {
//------------------------------------------------------------------------------
	struct Window_text_display : Widget {
		Window_text_display(Point XY, int W, int H, string L, Callback cb): Widget(XY, W, H, L, cb)
		{}
		void set_buffer(string s);
		void attach(Window&);
	private:
		Fl_Text_Buffer data;
		Fl_Text_Display *ptd;
		//constants for bar graph
		
	};
	void Window_text_display::attach(Window& win)
	{
		ptd = new Fl_Text_Display(loc.x, loc.y, width, height, label.c_str());
		ptd->buffer(&data);
		pw  = (Fl_Widget*) ptd;
		pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
		own = &win;
	}
	void Window_text_display::set_buffer(string s) 
	{
		data.text(s.c_str());
	}

//------------------------------------------------------------------------------
	struct Menu_bar : Widget {
		Menu_bar(Point XY, int W, int H, Callback cb): Widget(XY, W, H, "", cb)
		{}
		void menu(Fl_Menu_Item *ma);
		void attach(Window&);
	private:
		Fl_Menu_Bar *pmb;
		Fl_Menu_Item *ma;

	};
	void Menu_bar::attach(Window& win)
	{
		pmb = new Fl_Menu_Bar(loc.x, loc.y, width, height);
		pmb->menu(ma);
		pw  = (Fl_Widget*) pmb;
		pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
		own = &win;
	}
	void Menu_bar::menu(Fl_Menu_Item *m) 
	{
		ma = m;
	}
}

//header declarations
void do_about_box(Graph_lib::Window&);
void do_about_help(Graph_lib::Window&);
void do_findCust(Graph_lib::Window&,Store_menu);
void do_display_box(Graph_lib::Window&, Store_menu&, Msg_type);								/** MSH on B2 part II**/
void do_read(Graph_lib::Window&, Store_menu&, string, string, Msg_type);
void show_graph_cust(Graph_lib::Window&,Store_menu);										/** AGV on C3 part II**/
void Main_Window_CB(Fl_Widget*, void*);
void Menu_Bar_CB (Fl_Widget*, void*);

//global data
bool main_window_click = false;
bool menu_bar_click = false;
bool main_click_enabled = true;//force serialization on clicks
int window_userdata;
//constants
const int X_SCALE = 36;																	/** AGV on C3 part II **/
const int Y_SCALE = 60;																	/** AGV on C3 part II **/
const int BAR_WIDTH = 20;																/** AGV on C3 part II **/


//main window menu bar
//Note: for expediancy sake to define the menu bar below we used an FLTK type Fl_Menu_Item. 
//This makes our program  directly depend on FLTK instead of our interface Graph_lib.
//We don't like this, but we did it to save time, which often happens in the real world.
//A better solution is to write code to hide this in the implementation of our Graph_lib::Menu_bar
//An interesting exercise is to see if you can eliminate direct references in our code to FLTK types 
Fl_Menu_Item menu_bar[] = {
 {"Read", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Customers", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_customers), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Carts", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_carts), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Category/Products", 0,  (Fl_Callback*)Main_Window_CB, Address (Read_category_products), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Show", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Categories", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_categories), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Customers", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_customers), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Products", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_products), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Carts", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_carts), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Cart items", 0,  (Fl_Callback*)Main_Window_CB, Address (Show_cart_items), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Find", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Category Sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_category_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Customer sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_customer_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Product Sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Find_product_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Update", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Cart Item", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_cart_item), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Product", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_product), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Add Customer", 0,  (Fl_Callback*)Main_Window_CB, Address (Update_add_customer), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Display", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},																/** MSH on B2 part II**/
 {"Top Carts", 0,  (Fl_Callback*)Main_Window_CB, Address (Display_top_carts), 0, FL_NORMAL_LABEL, 0, 14, 0},		/** MSH on B2 part II**/
 {"Top Products", 0,  (Fl_Callback*)Main_Window_CB, Address (Display_top_products), 0, FL_NORMAL_LABEL, 0, 14, 0},	/** MSH on B2 part II**/
 {"Top Customers", 0,  (Fl_Callback*)Main_Window_CB, Address (Display_top3Cust), 0, FL_NORMAL_LABEL, 0, 14, 0},     /** MSH on B2 part II**/ /**AGV on B3 **/
 {0,0,0,0,0,0,0,0,0},																								/** MSH on B2 part II**/
 {"Graph", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},																/** AGV on C3 part II**/ /** MSH on C1 part II**/
 {"Carts sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Graph_carts_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},		/** AGV on C3 part II**/ /** MSH on C1 part II**/
 {"Customers sales", 0,  (Fl_Callback*)Main_Window_CB, Address (Graph_custs_sales), 0, FL_NORMAL_LABEL, 0, 14, 0},	/** AGV on C3 part II**/ /** MSH on C1 part II**/
 {0,0,0,0,0,0,0,0,0},
 {"About", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Info", 0,  (Fl_Callback*)Main_Window_CB, Address (About_info), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"Help", 0,  (Fl_Callback*)Main_Window_CB, Address (About_help), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {"Exit", 0,  0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
 {"Save and exit", 0,  (Fl_Callback*)Main_Window_CB, Address (Exit_program), 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0},
 {0,0,0,0,0,0,0,0,0}
};


//call back functions
void cust_cb(Address addr, Address) // callback for customer button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Customers_button));
}
void cart_cb(Address addr, Address) // callback for customer button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Carts_button));
}
void cat_cb(Address addr, Address) // callback for customer button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Categories_button));
}
void prod_cb(Address addr, Address) // callback for customer button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Products_button));
}
void cart_items_cb(Address addr, Address) // callback for customer button
{
	Main_Window_CB((Fl_Widget*) addr, Address (Cart_items_button));
}
 void display_cb(Address addr, Address) // callback for display window
{
	Main_Window_CB((Fl_Widget*) addr, Address (Display_window));
}
 void menu_bar_cb(Address addr, Address)//callback for menu bar
 {
	 Menu_Bar_CB((Fl_Widget*) addr, Address (Menu_bar_cb));
 }

void Menu_Bar_CB (Fl_Widget*, void* ud) {
	cout << "Menu Bar Callback" << " Userdata=" << (int) ud << "\n";
	menu_bar_click = true;
}

void Main_Window_CB(Fl_Widget*, void* ud) {
	if(main_click_enabled)//don't signal the window click if main loop is active
	{
		cout << "Main Window Callback" << " Userdata=" << (int) ud << "\n";
		window_userdata = (int) ud;
		main_window_click = true;
		main_click_enabled = false; //main loop active on current click
	}
}
//wait functions for callbacks to be executed
void wait_for_main_window_click() {
	while (!main_window_click) 
		Fl::wait();
	main_window_click = false;
	Fl::redraw();
}

void wait_for_menu_bar_click() {
	while (!menu_bar_click)
		Fl::wait();
	menu_bar_click = false;
	Fl::redraw();
}

int main()
{
	//create the main window and display it
	Store_menu m;
    Graph_lib::Window sw(600, 500, "OnLine Store Report Program");
    Button cust(Point(40, 50), 100, 30, "Customers", cust_cb);
	sw.attach(cust);
	Button cart(Point(40, 100), 100, 30, "Carts", cart_cb);
	sw.attach(cart);
	Button cat(Point(40, 150), 100, 30, "Categories", cat_cb);
	sw.attach(cat);
	Button prod(Point(40, 200), 100, 30, "Products", prod_cb);
	sw.attach(prod);
	Button cart_items(Point(40, 250), 100, 30, "Cart Items", cart_items_cb);
	sw.attach(cart_items);
    sw.callback((Fl_Callback*)Main_Window_CB, Address (Exit_program));
	Menu_bar mb(Point(0, 0), 600, 30, menu_bar_cb);
    mb.menu(menu_bar);
	sw.attach(mb);

	Window_text_display t(Point(150,50),400, 400, "Data Display", display_cb );
	t.set_buffer("No Data Loaded\n");
	sw.attach(t);
 	sw.resizable(&sw);
	

	//main loop to execute user clicks on the main window
	try {
		
		while(true) {
			main_click_enabled = true;
			wait_for_main_window_click();
			cout << "Act on userdata = " << window_userdata << endl;
			switch(window_userdata) 
			{
				//need to complete all the menu cases
				case Read_customers:
					do_read(sw, m, "cust.dat", "Open Customer File", Read_customers);
					break;
				case Read_carts:
					do_read(sw, m, "carts.dat", "Open Carts Data File", Read_carts);
					break;
				case Read_category_products:
					do_read(sw, m, "catprd.dat", "Open Catelog/Product File", Read_category_products);
					break;
				case About_info:
					do_about_box(sw);
					break;
				case About_help:
					do_about_help(sw);
					break;
				case Customers_button: case Carts_button: case Categories_button: case Cart_items_button: case Products_button:
					t.set_buffer(m.show_button((Msg_type) window_userdata).c_str());
					break;
				case Show_categories: case Show_customers: case Show_products:															/** MSH on A2 part II**/
				case Show_carts: case Show_cart_items:
					t.set_buffer(m.show_m((Msg_type) window_userdata).c_str());
					break;
				case Display_top_carts:
					do_display_box(sw, m, (Msg_type) window_userdata);
					break;
				case Display_top3Cust:
					do_findCust(sw,m);
					break;
				case Display_top_products:																								/** MSH on B2 part II**/
					do_display_box(sw, m, (Msg_type) window_userdata);																	/** MSH on B2 part II**/
					break;																												/** MSH on B2 part II**/
				case Graph_custs_sales:																									/** AGV on C3 part II**/
					show_graph_cust(sw,m);
					//show_graph_cust(sw,m);																								/** AGV on C3 part II**/
				case Exit_program:
					cout << "save files and exit\n";
					//exit(0); //exit program
					break;
				default:
					cout << "case not implemented\n";
			}
		}
		return 0;
	}
	catch(exception& e) {
		cerr << "exception: " << e.what() << '\n';
		return 1;
	}
	catch (...) {
		cerr << "Some exception\n";
		return 2;
	}
}
//show about box info window
void do_about_box(Graph_lib::Window& w) 
{
	Graph_lib::Window ab(Point(w.x()+100, w.y()+30), 700, 600, "About Box");
	ab.color(Color::white);
	ab.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_about_box));
	Text msg(Point(15,50), "This is OLES Store");
	msg.set_color(Color::red);
	ab.attach(msg);

	Text dev(Point(15,80), "Developed by : ");
	dev.set_color(Color::blue);
	ab.attach(dev);

	Text msh(Point(20,110), "Michell Setyawati Handaka - Institut Teknologi Bandung / 13508045 ");
	msh.set_color(Color::black);
	ab.attach(msh);

	Image m(Point(20,130),"michell.jpg"); // 400*212 pixel jpg
    ab.attach(m);
	

	Text agv(Point(20,300), "Achmad Giovani - Institut Teknologi Bandung / 13508073 ");
	agv.set_color(Color::black);
	ab.attach(agv);

	Image g(Point(20,320),"gio.jpg"); // 400*212 pixel jpg
    ab.attach(g);

	Text ogg(Point(20,500), "Christopher P. Ogg - Texas A&M University");
	ogg.set_color(Color::black);
	ab.attach(ogg);

	Text ogg2(Point(20,520), "<photo not available>");
	ogg2.set_color(Color::black);
	ab.attach(ogg2);
	

	wait_for_menu_bar_click();
	ab.detach(msg); ab.detach(dev); ab.detach(agv); ab.detach(msh); ab.detach(ogg);
	ab.detach(ogg2);//clean up window
	ab.detach(m); ab.detach(g);
	return;
}

//show about box help window
void do_about_help(Graph_lib::Window& w) 
{
	Graph_lib::Window ab(Point(w.x()+100, w.y()+100), 780, 400, "Help");
	ab.color(Color::white);
	ab.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_about_box));
	
	String row1 =  "How to use :\n";
	String row2 =  "1. Select menu \"Read\" to load data. Choose one of them (customer, category/products/cart)\n";
	String row3 =  "2. To show the data, you can either click on \"Show\" menu or buttons\n";
	String row4 =  "3. To find certain data record, click on the \"Find\" menu, choose one of them, and then enter the keywords\n";
	String row5 =  "4. To add/delete data record, click on the \"Update\" menu, choose one of them, and then enter the new values or ID\n";

	Text msg(Point(15,50), row1);
	Text msg2(Point(15,70), row2);
	Text msg3(Point(15,90), row3);
	Text msg4(Point(15,110), row4);
	Text msg5(Point(15,130), row5);
	msg.set_color(Color::black);
	ab.attach(msg); ab.attach(msg2); ab.attach(msg3); ab.attach(msg4); ab.attach(msg5);
	wait_for_menu_bar_click();
	ab.detach(msg); ab.detach(msg2); ab.detach(msg3); ab.detach(msg4); ab.detach(msg5); //clean up window
	return;
}

void do_findCust(Graph_lib::Window& w, Store_menu m){
	Graph_lib::Window ab(Point(w.x()+100, w.y()+100), 500, 200, "Top 3 Customers");
	ab.color(Color::white);
	ab.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_about_box));
	//get text from fintTop3Custs
	Text msg(Point(15,50), "Top 3 customers by purchase frequency :  ");
	
	Text msg2(Point(15,70), m.findTop3Custs());
	/*Text msg3(Point(15,90), ss2.str());
	Text msg4(Point(15,110), ss3.str());*/
	msg.set_color(Color::black);
	ab.attach(msg); ab.attach(msg2); /*ab.attach(msg3); ab.attach(msg4);*/
	wait_for_menu_bar_click();
	ab.detach(msg); ab.detach(msg2); /*ab.detach(msg3); ab.detach(msg4);*/ //clean up window
	return;
}


//show display box																			/** MSH on B2 part II**/
void do_display_box(Graph_lib::Window& w, Store_menu& m, Msg_type type) {					/** MSH on B2 part II**/
	String title, message;																	/** MSH on B2 part II**/
	string* messages;																		/** MSH on B2 part II**/
	int n;																					/** MSH on B2 part II**/

	switch (type) {																			/** MSH on B2 part II**/
		case Display_top_carts:																/** MSH on B2 part II**/
			title = "Display Top Carts";													/** MSH on B2 part II**/
	
			
			message = "The five top selling carts by largest checkout total:";
			n = 5;
			messages = m.getTopCarts(n);

			break;																			/** MSH on B2 part II**/
		case Display_top_products:															/** MSH on B2 part II**/
			title = "Display Top Products";													/** MSH on B2 part II**/

			/** MSH on B2 part II**/
			message = "The five top selling products by revenue regardless of category:";
			n = 5;
			messages = m.getTopProducts(n);

			break;																			/** MSH on B2 part II**/
		case Display_top_customers:															/** MSH on B2 part II**/
			title = "Display Top Customers";												/** MSH on B2 part II**/


			break;																			/** MSH on B2 part II**/
		default:																			/** MSH on B2 part II**/
			throw InvalidType();															/** MSH on B2 part II**/
	}																						/** MSH on B2 part II**/

	Graph_lib::Window ab(Point(w.x()+100, w.y()+100), 500, 200, title);						/** MSH on B2 part II**/
	ab.color(Color::white);																	/** MSH on B2 part II**/
	ab.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_display_box));					/** MSH on B2 part II**/


	Text msg(Point(15,50), message);														/** MSH on B2 part II**/
	msg.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg);																			/** MSH on B2 part II**/
	Text msg0(Point(15,70), 0 < n ? messages[0] : " ");										/** MSH on B2 part II**/
	msg0.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg0);																		/** MSH on B2 part II**/
	Text msg1(Point(15,90), 1 < n ? messages[1] : " ");										/** MSH on B2 part II**/
	msg1.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg1);																		/** MSH on B2 part II**/
	Text msg2(Point(15,110), 2 < n ? messages[2] : " ");									/** MSH on B2 part II**/
	msg2.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg2);																		/** MSH on B2 part II**/
	Text msg3(Point(15,130), 3 < n ? messages[3] : " ");									/** MSH on B2 part II**/
	msg3.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg3);																		/** MSH on B2 part II**/
	Text msg4(Point(15,150), 4 < n ? messages[4] : " ");									/** MSH on B2 part II**/
	msg4.set_color(Color::black);															/** MSH on B2 part II**/
	ab.attach(msg4);																		/** MSH on B2 part II**/
	wait_for_menu_bar_click();																/** MSH on B2 part II**/
	ab.detach(msg);//clean up window														/** MSH on B2 part II**/
	ab.detach(msg0);//clean up window														/** MSH on B2 part II**/
	ab.detach(msg1);//clean up window														/** MSH on B2 part II**/
	ab.detach(msg2);//clean up window														/** MSH on B2 part II**/
	ab.detach(msg3);//clean up window														/** MSH on B2 part II**/
	ab.detach(msg4);//clean up window														/** MSH on B2 part II**/

	delete [] messages;

	return;																					/** MSH on B2 part II**/
}																							/** MSH on B2 part II**/


void do_read(Graph_lib::Window& w, Store_menu& m, string dfn, string msg, Msg_type type)
{
	//Here again we tied our code directly to the FLTK library instead of creating an interface in our Graph_lib interface to FLTK
	//We did it for lack of time, which reflects the real word. Unfortunately if we wanted to move our code to a different graphics
	//library, we have to change our code and not just our Graphic_lib implementation.
	Fl_File_Chooser fc(dfn.c_str(), "*.dat", Fl_File_Chooser::CREATE, msg.c_str());
	fc.show();
	while (fc.shown())
		Fl::wait();	//wait for OK or Cancel button
	if(0!=fc.value())//make sure there is a string to use as a filename
	{
		string fname = fc.value();
		switch(type) {
			case Read_customers:
				m.read_custs(fname);
				break;
			case Read_carts:
				m.read_carts(fname);
				break;
			case Read_category_products:
				m.read_catprds(fname);
				break;
			default:
				throw InvalidType();
		}
	}
	return;
}


void show_graph_cust(Graph_lib::Window& w,Store_menu m){												/** AGV on C3 part II**/
	
	if (! m.isCustEmpty() && ! m.isCartEmpty() && ! m.isCatEmpty() && ! m.isProdEmpty() && ! m.isCartItemEmpty()) {					/** MSH on C1 part II**/

		//sales string																						/** AGV on C3 part II**/
		//call function from Store_menu.h
		string* sales_str = m.find_cust_sales();															/** AGV on C3 part II**/

		//popup window
		Graph_lib::Window gr(Point(w.x()+50, w.y()+50), 900, 600, "Graph for Customer Sales");				/** AGV on C3 part II**/
		gr.color(Color::white);																				/** AGV on C3 part II**/
		gr.callback((Fl_Callback*)Menu_Bar_CB, Address (Graph_custs_sales));								/** AGV on C3 part II**/
	
		//axis
	
		Axis ya(Axis::y, Point(140,450), Y_SCALE*6, 6, "Sales"); // 1 scale = 5000, max 30000				/** AGV on C3 part II**/
		ya.set_color(Color::blue);           // choose a color												/** AGV on C3 part II**/
		ya.label.set_color(Color::dark_red); // choose a color for the text									/** AGV on C3 part II**/
		Axis xa(Axis::x, Point(140,450), X_SCALE*15, 15, "Customer ID");	//# of cust = 14				/** AGV on C3 part II**/
		xa.set_color(Color::blue);           // choose a color												/** AGV on C3 part II**/
		xa.label.set_color(Color::dark_red); // choose a color for the text									/** AGV on C3 part II**/

		//legend
			//sales
		Text y1(Point(90,450),"0");																			/** AGV on C3 part II**/
		Text y2(Point(90,390),"5000");																		/** AGV on C3 part II**/
		Text y3(Point(90,330),"10000");																		/** AGV on C3 part II**/
		Text y4(Point(90,270),"15000");																		/** AGV on C3 part II**/
		Text y5(Point(90,210),"20000");																		/** AGV on C3 part II**/
		Text y6(Point(90,150),"25000");																		/** AGV on C3 part II**/
		Text y7(Point(90,90),"30000");																		/** AGV on C3 part II**/
			//customer id
		string cust_n;																						/** AGV on C3 part II**/
		for	(int i=0; i<14; ++i){
			int begin=3;																					/** AGV on C3 part II**/
			
			if (sales_str[i].substr(2,1) == " "){															/** AGV on C3 part II**/
			}
			else{
				begin++;																					/** AGV on C3 part II**/
			}
			string c = sales_str[i].substr(begin,4);														/** AGV on C3 part II**/

			cust_n += c + " ";																				/** AGV on C3 part II**/
		}

		Text c(Point(158,485),cust_n);																		/** AGV on C3 part II**/
		gr.attach(c);																						/** AGV on C3 part II**/

		//bar

		int x = 166;	//starting x for first bar															/** AGV on C3 part II**/
		for (int i=0; i<14; ++i){																			/** AGV on C3 part II**/
			//parse sales from string to double
			int begin_c = (int)sales_str[i].find_first_of("("); ++begin_c;									/** AGV on C3 part II**/
			int end_c = (int)sales_str[i].find_first_of(")");	--end_c;									/** AGV on C3 part II**/
			int num = end_c - begin_c + 1;																	/** AGV on C3 part II**/
			string str = sales_str[i].substr(begin_c,num);													/** AGV on C3 part II**/
			char* str_c = (char*) str.c_str();																/** AGV on C3 part II**/
			double sales = strtod(str_c,NULL);																/** AGV on C3 part II**/
		
			//define start point for each bar
			int y = 450 - (int)(sales*Y_SCALE/5000);														/** AGV on C3 part II**/
		
			//create rectangle
			Graph_lib::Rectangle *r;																		/** AGV on C3 part II**/
			r = new Graph_lib::Rectangle(Point(x,y), BAR_WIDTH, (int)(sales*Y_SCALE/5000));					/** AGV on C3 part II**/
			r->set_fill_color(Color::dark_green);															/** AGV on C3 part II**/
		
			//attach
			gr.attach(*r);																					/** AGV on C3 part II**/
		
			//next bar
			x += 36;																						/** AGV on C3 part II**/
		}	

		gr.attach(ya); gr.attach(xa); //gr.attach(max);																/** AGV on C3 part II**/
		gr.attach(y1); gr.attach(y2); gr.attach(y3); gr.attach(y4); gr.attach(y5); gr.attach(y6); gr.attach(y7);	/** AGV on C3 part II**/
	
		wait_for_menu_bar_click();																					/** AGV on C3 part II**/

		gr.detach(ya); gr.detach(xa); //gr.detach(max);																/** AGV on C3 part II**/
		gr.detach(y1); gr.detach(y2); gr.detach(y3); gr.detach(y4); gr.detach(y5); gr.detach(y6); gr.detach(y7);	/** AGV on C3 part II**/
		gr.detach(c);																								/** AGV on C3 part II**/
	
	} else {																														/** MSH on C1 part II**/
		Graph_lib::Window mb(Point(w.x()+100, w.y()+100), 400, 120, "Message Box");													/** MSH on C1 part II**/
		mb.color(Color::white);																										/** MSH on C1 part II**/
		mb.callback((Fl_Callback*)Menu_Bar_CB, Address (Close_message_box));														/** MSH on C1 part II**/
		Text msg(Point(15,50), "no data loaded");																					/** MSH on C1 part II**/
		msg.set_color(Color::black);																								/** MSH on C1 part II**/
		mb.attach(msg);																												/** MSH on C1 part II**/
		wait_for_menu_bar_click();																									/** MSH on C1 part II**/
		mb.detach(msg);//clean up window																							/** MSH on C1 part II**/
	}																																/** MSH on C1 part II**/

	return;																										/** AGV on C3 part II**/
}